package com.kstech.test.padxmledit;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kstech.test.padxmledit.domain.QCItem;

import java.util.LinkedList;

/**
 * Created by lenovo on 2016/9/7.
 */
public class EditFragMent extends Fragment {
    private TextView name;
    private TextView Requires;
    private EditText QCTimes;
    private EditText ReadyTimeOut;
    private EditText QCTimeOut;
    private LinearLayout ll_params;
    private Button btnEnsure;
    private EditListFragMent elf;
    private String sQctimes;
    private String sReadyTimeOut;
    private String sQCTimeOut;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.data_edit,null);
        Globals.viewList.clear();
        init(view);
        elf = (EditListFragMent) getFragmentManager().findFragmentByTag("EditListFragMent");
        if(Globals.qcItem!=null){
            name.setText(Globals.qcItem.getName());
            Requires.setText(Globals.qcItem.getRequire());
            QCTimes.setText(Globals.qcItem.getQCTimes());
            ReadyTimeOut.setText(Globals.qcItem.getReadyTimeout());
            QCTimeOut.setText(Globals.qcItem.getQCTimeout());
            for (QCItem.QCParam param:Globals.qcItem.getQCParams()) {
                QCparamView qcview = new QCparamView(getActivity(),param);
                ll_params.addView(qcview);
                Globals.viewList.add(qcview);
            }
            btnEnsure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sQctimes = QCTimes.getText().toString().trim();
                    sQCTimeOut = QCTimeOut.getText().toString().trim();
                    sReadyTimeOut = ReadyTimeOut.getText().toString().trim();
                    if(sQctimes==null || sQCTimeOut==null ||sReadyTimeOut == null ||"".equals(sReadyTimeOut)
                            ||"".equals(sQCTimeOut)||"".equals(sReadyTimeOut)){
                        Toast.makeText(getActivity(),"信息未完善，会影响检测程序，请完善信息",Toast.LENGTH_SHORT).show();
                        return;
                    }else {
                        Globals.qcItem.setQCTimes(sQctimes);
                        Globals.qcItem.setQCTimeout(sQCTimeOut);
                        Globals.qcItem.setReadyTimeout(sReadyTimeOut);
                        LinkedList<QCItem.QCParam> params = new LinkedList<QCItem.QCParam>();
                        for (QCparamView qv:Globals.viewList) {
                            if(qv.getParam().ValidMax==null ||qv.getParam().ValidMin==null){
                                Toast.makeText(getActivity(),"信息未完善，会影响检测程序，请完善信息",Toast.LENGTH_SHORT).show();
                                return;
                            }else {
                                params.add(qv.getParam());
                            }
                        }
                        Globals.qcItem.setQCParams(params);
                        Globals.viewList.clear();
                        elf.handler.sendEmptyMessage(0);
                    }
                }
            });
        }
        return view;
    }
    public void init(View view){
        name = (TextView) view.findViewById(R.id.name);
        Requires = (TextView) view.findViewById(R.id.requires);
        QCTimes = (EditText) view.findViewById(R.id.qctimes);
        ReadyTimeOut = (EditText) view.findViewById(R.id.readytimeout);
        QCTimeOut = (EditText) view.findViewById(R.id.qctimeout);
        ll_params = (LinearLayout) view.findViewById(R.id.ll_params);
        btnEnsure = (Button) view.findViewById(R.id.ensure);
    }
}
